import unittest

import kivy
from kivy.lang import Builder

from ui.presenter_mock import PresenterMock
from ui.load_game import LoadGameScreen
from model.gamestate import Phase, GameState
from model.json_loader import JsonLoader
import mox


class LoadGameTest(unittest.TestCase):
    def setUp(self):
        self.presenter = PresenterMock(Phase.START_GAME,
                                       Phase.WELCOME)
        self.gamestate = GameState(None, None, None,
                                   None, None, None, None)

    def test_prev_screen(self):
        widget = LoadGameScreen(self.gamestate, self.presenter)
        self.assertFalse(self.presenter.is_phase_changed)
        widget.go_back()
        self.assertTrue(self.presenter.is_phase_changed)

    def test_checkers(self):
        widget = LoadGameScreen(self.gamestate, self.presenter)
        widget.game_id = u''
        widget.pin_code = u''
        self.assertFalse(widget.load_button.enabled)
        self.assertFalse(widget.is_correct_pin)
        self.assertFalse(widget.is_correct_id)
        widget.game_id = u'1'
        self.assertFalse(widget.load_button.enabled)
        self.assertFalse(widget.is_correct_pin)
        self.assertTrue(widget.is_correct_id)
        widget.pin_code = u'1234'
        self.assertTrue(widget.load_button.enabled)
        self.assertTrue(widget.is_correct_pin)
        self.assertTrue(widget.is_correct_id)
        widget.game_id = u''
        self.assertFalse(widget.load_button.enabled)
        self.assertTrue(widget.is_correct_pin)
        self.assertFalse(widget.is_correct_id)
        widget.pin_code = u'12345'
        widget.game_id = u'1'
        self.assertTrue(widget.load_button.enabled)
        self.assertTrue(widget.is_correct_pin)
        self.assertTrue(widget.is_correct_id)
        widget.pin_code = u'1234567'
        widget.game_id = u'1'
        self.assertFalse(widget.load_button.enabled)
        self.assertFalse(widget.is_correct_pin)
        self.assertTrue(widget.is_correct_id)
        widget.pin_code = u'12'
        self.assertFalse(widget.load_button.enabled)
        self.assertFalse(widget.is_correct_pin)
        self.assertTrue(widget.is_correct_id)

    def test_server_and_finish(self):
        widget = LoadGameScreen(self.gamestate, self.presenter)
        self.assertFalse(self.presenter.is_phase_changed)

        # fail with wrong id
        widget.game_id = u'1000000'
        widget.pin_code = u'0000'
        widget.on_load_pressed()
        self.assertFalse(self.presenter.is_phase_changed)
        self.assertTrue(widget.game_state.remote_id is None)
        self.assertTrue(widget.game_state.pin_code is None)

        # fail with wrong pin
        widget.game_id = u'1'
        widget.pin_code = u'0000'
        widget.on_load_pressed()
        self.assertFalse(self.presenter.is_phase_changed)
        self.assertTrue(widget.game_state.remote_id is None)
        self.assertTrue(widget.game_state.pin_code is None)

    def test_right_game(self):
        # right game
        widget = LoadGameScreen(self.gamestate, self.presenter)

        m = mox.Mox()
        m.StubOutWithMock(JsonLoader, 'load_game_from_url')
        return_gamestate = GameState(None, None, None, None, None, None, None)
        return_gamestate.remote_id = 1
        JsonLoader.load_game_from_url(
            self.presenter.settings.game.server_name +
            u'/game_template_json?game_id=1&pin=0000')\
            .AndReturn(return_gamestate)
        m.ReplayAll()

        widget.input_id.text = u'1'
        widget.input_pin.text = u'0000'
        widget.on_load_pressed()
        print widget.game_state
        print self.gamestate
        print self.presenter.game_state
        print return_gamestate
        self.assertEqual(widget.game_state.remote_id, 1)
        self.assertEqual(widget.game_state.pin_code, 0000)
        self.assertTrue(self.presenter.is_phase_changed)
        m.VerifyAll()

if __name__ == '__main__':
    unittest.main()
